package cn.com.headfree.workflow.service.impl;

import java.util.List;

import cn.com.headfree.userinfo.dao.IUserInfoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gta.edu.sdk.common.dto.Page;

import cn.com.headfree.entity.TEducateInfo;
import cn.com.headfree.entity.TEmpBaseInfo;
import cn.com.headfree.entity.TEmployeeMobility;
import cn.com.headfree.entity.TMouldInfo;
import cn.com.headfree.entity.TOrgDept;
import cn.com.headfree.entity.TPartRank;
import cn.com.headfree.entity.TSdtPrentInfo;
import cn.com.headfree.entity.TYearEffectInfo;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.mouldshow.dao.HumanDataDao;
import cn.com.headfree.recordlog.dao.IRecordLogDao;
import cn.com.headfree.vo.WaitDoVo;
import cn.com.headfree.vo.workflow.DbCount;
import cn.com.headfree.vo.workflow.HadDoListVo;
import cn.com.headfree.vo.workflow.WaitDoListVo;
import cn.com.headfree.workflow.dao.IWorkFlowDao;
import cn.com.headfree.workflow.service.IWorkFlowService;

@Service("workFlowService")
public class WorkFlowServiceImpl implements IWorkFlowService {

	@Autowired
	public IWorkFlowDao workFlowDao;
	@Autowired
	private HumanDataDao humanDataDao;
	@Autowired
	private IUserInfoDao userInfoDao;
	@Autowired
	private IRecordLogDao recordLogDao;

	@Override
	public void findWaitDoPageByCondition(Page<WaitDoVo> page, WaitDoVo waitDo) {
//		StringBuilder sqlbuilder = new StringBuilder();
//		ArrayList<Object> arrayList = new ArrayList<Object>();
//		sqlbuilder.append("select a.*,b.name as user_name from "+waitDo.getTabName()+" a inner join t_emp_base_info b on a.erp_no=b.erp_no inner join t_dept_emp_rela c");
//		sqlbuilder.append(" on a.erp_no=c.erp_no inner join ");
//		if (!StringUtil.isNullOrEmpty(inTimeBeginStr)) {
//			arrayList.add(inTimeBeginStr);
//			arrayList.add(inTimeBeginStr);
//		}
//		sqlbuilder.append("(select @rownum:=@rownum+1 as m from ");
//		sqlbuilder.append("(select 1 union select 2 union select 3 union select 4) t1, ");
//		sqlbuilder.append("(select 1 union select 2 union select 3 union select 4) t2, ");
//		sqlbuilder.append("(select 1 union select 2 union select 3 union select 4) t3, ");
//		sqlbuilder.append("(select 1 union select 2 union select 3 union select 4) t4, ");
//		sqlbuilder.append("(select @rownum:=-1) t0) d1) d2  ");
//		sqlbuilder.append("where m1<=? ");
//		if (!StringUtil.isNullOrEmpty(inTimeEndStr)) {
//			arrayList.add(inTimeEndStr);
//		}
//		sqlbuilder.append("order by m1) as a left join ");
//		sqlbuilder.append(
//				"(select DATE_FORMAT(in_time, ?) as 'in',count(in_time) as 'incount' from t_attendance_user_inout_info where 1=1 ");
//		arrayList.add("%Y-%m");
//		if (!StringUtil.isNullOrEmpty(projectId)) {
//			sqlbuilder.append(" AND project_id=? ");
//			arrayList.add(projectId);
//		}
//		if (!StringUtil.isNullOrEmpty(supplierId)) {
//			sqlbuilder.append(" AND supplier_id=? ");
//			arrayList.add(supplierId);
//		}
//		sqlbuilder.append(" group by DATE_FORMAT(in_time, ?)) as b on a.time=b.in ");
//		arrayList.add("%Y-%m");
//		sqlbuilder.append("left join( ");
//		sqlbuilder.append(
//				"select DATE_FORMAT(leave_time, ?) as 'out',count(leave_time) as 'outcount' from t_attendance_user_inout_info where 1=1 ");
//		arrayList.add("%Y-%m");
//		if (!StringUtil.isNullOrEmpty(projectId)) {
//			sqlbuilder.append(" AND project_id=? ");
//			arrayList.add(projectId);
//		}
//		if (!StringUtil.isNullOrEmpty(supplierId)) {
//			sqlbuilder.append(" AND supplier_id=? ");
//			arrayList.add(supplierId);
//		}
//		sqlbuilder.append(" group by DATE_FORMAT(leave_time, ?)) as c  ");
//		arrayList.add("%Y-%m");
//		sqlbuilder.append(" on a.time=c.out order by time");
//		this.findPageBySql(InOutUserCount.class, sqlbuilder.toString(), page, arrayList.toArray());

	}

	@Override
	public List<TOrgDept> getDeptsByOrgId(User user) {
		return workFlowDao.getDeptsByOrgId(user);
	}

	@Override
	public void findWaitDoBaseInfoPageByCondition(Page<WaitDoListVo> page, WaitDoListVo waitDo) {
		waitDo.setCurrentLoginRoleCode(spiltStr(waitDo.getCurrentLoginRoleCode()));
		workFlowDao.findWaitDoBaseInfoPageByCondition(page, waitDo);
	}

	@Override
	public void findWaitDoYearEffectInfoPageByCondition(Page<WaitDoListVo> page, WaitDoListVo waitDo) {
		waitDo.setCurrentLoginRoleCode(spiltStr(waitDo.getCurrentLoginRoleCode()));
		workFlowDao.findWaitDoYearEffectInfoPageByCondition(page, waitDo);
	}

	@Override
	public void findWaitDoEducationInfoPageByCondition(Page<WaitDoListVo> page, WaitDoListVo waitDo) {
		waitDo.setCurrentLoginRoleCode(spiltStr(waitDo.getCurrentLoginRoleCode()));
		workFlowDao.findWaitDoEducationInfoPageByCondition(page, waitDo);
	}

	@Override
	public void findWaitDoContiStudyInfoPageByCondition(Page<WaitDoListVo> page, WaitDoListVo waitDo) {
		waitDo.setCurrentLoginRoleCode(spiltStr(waitDo.getCurrentLoginRoleCode()));
		workFlowDao.findWaitDoContiStudyInfoPageByCondition(page, waitDo);
	}

	@Override
	public void findWaitDoProTecInfoPageByCondition(Page<WaitDoListVo> page, WaitDoListVo waitDo) {
		waitDo.setCurrentLoginRoleCode(spiltStr(waitDo.getCurrentLoginRoleCode()));
		workFlowDao.findWaitDoProTecInfoPageByCondition(page, waitDo);
	}

	@Override
	public void findWaitDoExpertTitleInfoPageByCondition(Page<WaitDoListVo> page, WaitDoListVo waitDo) {
		waitDo.setCurrentLoginRoleCode(spiltStr(waitDo.getCurrentLoginRoleCode()));
		workFlowDao.findWaitDoExpertTitleInfoPageByCondition(page, waitDo);
	}

	@Override
	public void findWaitDoSkillLevelInfoPageByCondition(Page<WaitDoListVo> page, WaitDoListVo waitDo) {
		waitDo.setCurrentLoginRoleCode(spiltStr(waitDo.getCurrentLoginRoleCode()));
		workFlowDao.findWaitDoSkillLevelInfoPageByCondition(page, waitDo);
	}

	@Override
	public void findWaitDoAcadeAchiveInfoPageByCondition(Page<WaitDoListVo> page, WaitDoListVo waitDo) {
		waitDo.setCurrentLoginRoleCode(spiltStr(waitDo.getCurrentLoginRoleCode()));
		workFlowDao.findWaitDoAcadeAchiveInfoPageByCondition(page, waitDo);
	}

	@Override
	public void findWaitDoStandInfoPageByCondition(Page<WaitDoListVo> page, WaitDoListVo waitDo) {
		waitDo.setCurrentLoginRoleCode(spiltStr(waitDo.getCurrentLoginRoleCode()));
		workFlowDao.findWaitDoStandInfoPageByCondition(page, waitDo);
	}

	@Override
	public void findWaitDoPatentInfoPageByCondition(Page<WaitDoListVo> page, WaitDoListVo waitDo) {
		waitDo.setCurrentLoginRoleCode(spiltStr(waitDo.getCurrentLoginRoleCode()));
		workFlowDao.findWaitDoPatentInfoPageByCondition(page, waitDo);
	}

	@Override
	public void findWaitDoTecInnovInfoPageByCondition(Page<WaitDoListVo> page, WaitDoListVo waitDo) {
		waitDo.setCurrentLoginRoleCode(spiltStr(waitDo.getCurrentLoginRoleCode()));
		workFlowDao.findWaitDoTecInnovInfoPageByCondition(page, waitDo);
	}

	@Override
	public void findWaitDoContestInfoPageByCondition(Page<WaitDoListVo> page, WaitDoListVo waitDo) {
		waitDo.setCurrentLoginRoleCode(spiltStr(waitDo.getCurrentLoginRoleCode()));
		workFlowDao.findWaitDoContestInfoPageByCondition(page, waitDo);
	}

	@Override
	public void findWaitDoTecManInnovPageByCondition(Page<WaitDoListVo> page, WaitDoListVo waitDo) {
		waitDo.setCurrentLoginRoleCode(spiltStr(waitDo.getCurrentLoginRoleCode()));
		workFlowDao.findWaitDoTecManInnovPageByCondition(page, waitDo);
	}

	@Override
	public void findWaitDoHonorInfoPageByCondition(Page<WaitDoListVo> page, WaitDoListVo waitDo) {
		waitDo.setCurrentLoginRoleCode(spiltStr(waitDo.getCurrentLoginRoleCode()));
		workFlowDao.findWaitDoHonorInfoPageByCondition(page, waitDo);
	}

	@Override
	public void findWaitDoProjAchieveInfoPageByCondition(Page<WaitDoListVo> page, WaitDoListVo waitDo) {
		waitDo.setCurrentLoginRoleCode(spiltStr(waitDo.getCurrentLoginRoleCode()));
		workFlowDao.findWaitDoProjAchieveInfoPageByCondition(page, waitDo);
	}

	@Override
	public void findWaitDoSdtTutorPrentInfoPageByCondition(Page<WaitDoListVo> page, WaitDoListVo waitDo) {
		waitDo.setCurrentLoginRoleCode(spiltStr(waitDo.getCurrentLoginRoleCode()));
		workFlowDao.findWaitDoSdtTutorPrentInfoPageByCondition(page, waitDo);
	}

	@Override
	public void findWaitDoWorkExpInfoPageByCondition(Page<WaitDoListVo> page, WaitDoListVo waitDo) {
		waitDo.setCurrentLoginRoleCode(spiltStr(waitDo.getCurrentLoginRoleCode()));
		workFlowDao.findWaitDoWorkExpInfoPageByCondition(page, waitDo);
	}
	
	@Override
	public void findWaitDoRotationInfoPageByCondition(Page<WaitDoListVo> page, WaitDoListVo waitDo) {
		waitDo.setCurrentLoginRoleCode(spiltStr(waitDo.getCurrentLoginRoleCode()));
		workFlowDao.findWaitDoRotationInfoPageByCondition(page, waitDo);
	}

	@Override
	public void getHadDoListByCondition(Page<HadDoListVo> page, HadDoListVo hadDo) {
		hadDo.setCurrentLoginRoleCode(spiltStr(hadDo.getCurrentLoginRoleCode()));
		workFlowDao.getHadDoListByCondition(page, hadDo);
	}

	@Override
	public void updateBaseInfoYes(TEmpBaseInfo baseInfo) {
		workFlowDao.updateBaseInfoYes(baseInfo);
	}

	@Override
	public void examineTableYes(String businessId, String erpNo, String userName, String orignalId, String dealOpinion,
			String tabName) {
		// 将业务数据更改为有效数据0，并将流程审核状态改为通过1
		workFlowDao.updateTableValidByBusinessId(businessId, erpNo, userName, 0, orignalId, 1, tabName);
		// 将原有效数据更改为无效数据
		if (!businessId.equals(orignalId)) {
			workFlowDao.updateTableValidByBusinessId(businessId, erpNo, userName, 1, orignalId, null, tabName);
		}
		// 更新流程表信息
		workFlowDao.updateWorkFlow(businessId, dealOpinion, erpNo, userName);
	}

	@Override
	public void examineTableNo(String businessId, String tabName, String dealOpinion) {
		// 将业务数据流程审核状态改为不通过2
		workFlowDao.updateTableFlowStatusByBusinessId(businessId, 2, tabName);
		// 更新流程表信息
		workFlowDao.updateWorkFlow(businessId, dealOpinion);
	}

	@Override
	public void updateYearEffect(TYearEffectInfo yearEffectInfo, User user) {
		workFlowDao.updateYearEffect(yearEffectInfo, user);
	}

	@Override
	public void updateEducateInfoYes(TEducateInfo educateInfo, User user) {
		workFlowDao.updateEducateInfoYes(educateInfo, user);
	}

	@Override
	public TMouldInfo getMouldNameById(String id) {
		return workFlowDao.getMouldNameById(id);
	}

	@Override
	public void deleteTPartRankByCondition(String id) {
		humanDataDao.deleteTPartRankByCondition(null, id);
	}

	@Override
	public void addPartRank(TPartRank tPartRank) {
		userInfoDao.addPartRank(tPartRank);
	}

	@Override
	public void deleteTSdtPrentInfoByCondition(String id) {
		humanDataDao.deleteTSdtPrentInfoByCondition(null, id);
	}

	@Override
	public void addSdtPrentInfo(TSdtPrentInfo tSdtPrentInfo) {
		userInfoDao.addSdtPrentInfo(tSdtPrentInfo);
	}

	@Override
	public List<DbCount> getDbOrYbCountByBusinessType(String fax, String status) {
		return workFlowDao.getDbOrYbCountByBusinessType(spiltStr(fax), status);
	}

	@Override
	public void deleteTSdtPrentInfoByCondition(String orgId, String id) {
		humanDataDao.deleteTSdtPrentInfoByCondition(null, id);
	}

	@Override
	public void examineTableNo(String businessId, String tabName, String dealOpinion, String fax, String name) {
		// 将业务数据流程审核状态改为不通过2
		workFlowDao.updateTableFlowStatusByBusinessId(businessId, 2, tabName);
		// 更新流程表信息
		workFlowDao.updateWorkFlow(businessId, dealOpinion, spiltStr(fax), name);
	}

	public String spiltStr(String str) {
		StringBuffer sb = new StringBuffer();
		if (str != null && str.contains(",")) {
			String[] temp = str.split(",");
			for (int i = 0; i < temp.length; i++) {
				if (!"".equals(temp[i]) && temp[i] != null)
					sb.append("'" + temp[i] + "',");
			}
			String result = sb.toString();
			String tp = result.substring(result.length() - 1, result.length());
			if (",".equals(tp))
				return result.substring(0, result.length() - 1);
			else
				return result;
		}else {
			return str;
		}
	}

	@Override
	public void updateTEmployeeMobility(TEmployeeMobility tEmployeeMobility, User user) {
		workFlowDao.updateTEmployeeMobility(tEmployeeMobility,user);
	}

}